// 目标：过滤链表中的正数节点
// TODO: 补全 filter_positive 函数
struct ListNode {
    val: i32,
    next: Option<Box<ListNode>>,
}


impl ListNode {
    fn new(val: i32) -> Self {
        ListNode { val, next: None }
    }

    // 链表构建辅助函数
    fn from_vec(values: &[i32]) -> Option<Box<Self>> {
        let mut head = None;
        for &val in values.iter().rev() {
            let mut node = ListNode::new(val);
            node.next = head;
            head = Some(Box::new(node));
        }
        head
    }

    // 链表转Vec辅助函数
    fn to_vec(&self) -> Vec<i32> {
        let mut result = vec![self.val];
        let mut current = &self.next;
        while let Some(node) = current {
            result.push(node.val);
            current = &node.next;
        }
        result
    }
}


fn filter_positive(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
    // 你的代码（使用迭代或递归）
    let mut current = head?;
    
    // 递归处理后续节点
    let next = filter_positive(current.next.take());
    
    if current.val > 0 {
        current.next = next;
        Some(current)
    } else {
        next
    }
    //None // 占位
}

#[test]
fn test_filter() {
    // 测试用例需学生补充
    // 测试正常过滤
    let list = ListNode::from_vec(&[1, -2, 3, -4]);
    let filtered = filter_positive(list);
    assert_eq!(filtered.unwrap().to_vec(), vec![1, 3]);

    // 测试全负数
    let list = ListNode::from_vec(&[-1, -5]);
    assert!(filter_positive(list).is_none());

    // 测试边界值0
    let list = ListNode::from_vec(&[0, 5, -3]);
    assert_eq!(filter_positive(list).unwrap().to_vec(), vec![5]);
}

// 演示链表操作
fn main() {
    let original = ListNode::from_vec(&[7, -1, 8, -2, 9]);
    let filtered = filter_positive(original);
    
    println!("Original: {:?}", ListNode::from_vec(&[7, -1, 8, -2, 9]).unwrap().to_vec());
    println!("Filtered: {:?}", filtered.unwrap().to_vec());
}
